<!--学生信息管理-->
<template>
  <div class="exam">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formInline.key" placeholder="请输入学号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>

      <el-button type="primary" @click="addDialog = true">添加用户</el-button>
      <el-dialog title="添加用户" :visible.sync="addDialog">
        <el-form v-model="addForm">

          <el-form-item label="学号" :label-width="formLabelWidth">
            <el-input v-model="addForm.Name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="addForm.realname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-select v-model="addForm.Gender" placeholder="请选择性别">
              <el-option label="男" value="男"></el-option>
              <el-option label="女" value="女"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="学历" :label-width="formLabelWidth">
            <el-select v-model="addForm.EducationID" placeholder="请选择性别">
              <el-option label="本科" value="1"></el-option>
              <el-option label="硕士" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="专业" :label-width="formLabelWidth">
            <el-select v-model="addForm.MajorID" placeholder="请选择性别">
              <el-option label="计算机科学与技术" value="1"></el-option>
              <el-option label="电子信息" value="2"></el-option>
              <el-option label="大数据" value="3"></el-option>
              <el-option label="物联网" value="4"></el-option>
              <el-option label="数字媒体" value="5"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系方式" :label-width="formLabelWidth">
            <el-input v-model="addForm.ContactInfo" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电子邮箱" :label-width="formLabelWidth">
            <el-input v-model="addForm.email" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addDialog = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </div>
      </el-dialog>

      <el-button type="primary" @click="getUserInfo">重置</el-button>
    </el-form>
    <el-table
      :data="form"
      style="width: 100%"
      border>
      <el-table-column
        label="学生姓名"
        width="180">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.realname }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="Name"
        label="学号">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.Name }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="Gender"
        label="性别">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.Gender }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="ContactInfo"
        label="联系方式">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.ContactInfo }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="email"
        label="电子邮箱">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.email }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="编辑">
        <template v-slot="scope">
          <!--        编辑对话框-->
          <el-button type="primary" icon="el-icon-edit" @click="openEditDialog(scope.$index)"></el-button>
          <el-dialog title="编辑信息" :visible.sync="dialogVisibles[scope.$index]">

            <el-form v-model="editForm">
              <el-form-item label="姓名" :label-width="formLabelWidth">
                <el-input v-model="editForm.realname" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="性别" :label-width="formLabelWidth">
                <el-select v-model="editForm.Gender" placeholder="请选择类型">
                  <el-option label="男" value="男"></el-option>
                  <el-option label="女" value="女"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="学历" :label-width="formLabelWidth">
                <el-select v-model="editForm.EducationID" placeholder="请选择性别">
                  <el-option label="本科" value="1"></el-option>
                  <el-option label="硕士" value="2"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="专业" :label-width="formLabelWidth">
                <el-select v-model="editForm.MajorID" placeholder="请选择性别">
                  <el-option label="计算机科学与技术" value="1"></el-option>
                  <el-option label="电子信息" value="2"></el-option>
                  <el-option label="大数据" value="3"></el-option>
                  <el-option label="物联网" value="4"></el-option>
                  <el-option label="数字媒体" value="5"></el-option>
                </el-select>
              </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogVisibles[scope.$index] = false">取 消</el-button>
              <el-button type="primary" @click="Edit(scope.$index,scope.row)">确 定</el-button>
            </div>
          </el-dialog>

          <!--        删除对话框-->
          <el-button type="primary" icon="el-icon-delete" @click="openDeleteDialog(scope.$index)"></el-button>
          <el-dialog
            title="提示"
            :visible.sync="DeleteDialogVisibles[scope.$index]"
            width="30%">
            <span>确定删除信息？</span>
            <span slot="footer" class="dialog-footer">
            <el-button @click="DeleteDialogVisibles[scope.$index] = false">取 消</el-button>
            <el-button type="primary" @click="Delete(scope.$index,scope.row)">确 定</el-button>
          </span>
          </el-dialog>
        </template>

      </el-table-column>

    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[5, 10]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      class="page"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  name: "userManage",
  data() {
    return {
      key:'',
      addForm:{
        EducationID:"",
        MajorID:"",
        Name:"",
        realname:"",
        Gender:"",
        ContactInfo:"",
        Password:"",
        email:"",
        img:""
      },
      editForm:{
        Name:'',
        realname:'',
        Gender:'',
        EducationID:'',
        MajorID:'',
        img:'',
      },
      form: [],
      pagination: { //分页后的考试信息
        current: 1, //当前页
        total: null, //记录条数
        size: 5 //每页条数
      },
      formInline: {
        key: '',
        type: ''
      },
      addDialog:false,
      formLabelWidth:'90px',
      dialogVisibles:{},
      DeleteDialogVisibles:{}
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getUserInfo() { //分页查询所有用户信息
      this.$axios({
        url:'/api/teacher/list',
        method:'get',
        params:{
          page:this.pagination.current,
          size:this.pagination.size
        },
        headers:{
          'Authorization':this.$cookies.get("token")
        }
      }).then(res => {
        this.pagination = res.data.data
        this.form = res.data.data.records
        // console.log(this.pagination.current)
        // console.log(this.pagination.size)
        // console.log(this.form)
      })
    },
    //改变当前记录条数
    handleSizeChange(val) {
      this.pagination.size = val
      this.getUserInfo()
    },
    //改变当前页码，重新发送请求
    handleCurrentChange(val) {
      this.pagination.current = val
      this.getUserInfo()
    },
    //新增用户
    addUser(){
      this.addDialog = false
      this.addForm.Password = '123456'//默认密码
      this.$axios({
        url:'/api/teacher/addUser',
        method:'post',
        data:this.addForm,
        headers:{
          'Authorization':this.$cookies.get('token')
        }
      }).then(res =>{
        if(res.data.code === 0){
          this.$message.success("添加成功")
        }else{
          this.$message.error("添加失败")
        }
      })
      this.getUserInfo()
    },
    //查询用户
    onSubmit(){
    this.$axios({
      url:'/api/teacher/findStudentById',
      method:'get',
      params:{
        key:this.formInline.key
      },
      headers:{
        'Authorization':this.$cookies.get('token')
      }
    }).then(res =>{
      // console.log(res.data)
      this.pagination = res.data.data
      this.form = res.data.data.records
    })
    },
    //删除用户
    Delete(index,row){
      this.DeleteDialogVisibles[this.form.indexOf(row)] = false
      this.$axios({
        url:'/api/teacher/deleteUser',
        method:'delete',
        params:{id:this.form[index].Name},
        headers:{
          'Authorization':this.$cookies.get('token')
        }
      }).then(res =>{
        console.log(res.data)
        if(res.data.code === 0){
          this.$message.success("删除成功")
        }else {
          this.$message.error("删除失败")
        }
      })
      this.getUserInfo()
    },
    //修改信息
    Edit(index,row){
      // this.editDialog = false
      this.editForm.Name = this.form[index].Name
      this.dialogVisibles[this.form.indexOf(row)] = false
      console.log(index)
      this.$axios({
        url:'/api/user/update',
        method:'put',
        data:this.editForm,
        headers:{
          'Authorization':this.$cookies.get('token')
        }
      }).then(res =>{
        if(res.data.code === 0){
          this.$message.success("信息更新成功")
        }else{
          this.$message.error("更新失败")
        }
      })
      this.getUserInfo()

    },

    openEditDialog(index){
      this.$set(this.dialogVisibles,index,true)
    },
    openDeleteDialog(index){
      this.$set(this.DeleteDialogVisibles,index,true)
    }
  },
}
</script>

<style lang="less" scoped>
.exam {
  padding: 0px 40px;
 .page {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}
 .edit{
  margin-left: 20px;
 }
}
</style>
